$(function(){
	
	document.documentElement.style.fontSize = window.innerWidth/3.75 + 'px';
	
	var giftId;
	var userId;
	
	if(isIOS()){
		userId = getQueryStr('userId');
	}else{
		userId = JavascriptInterface.getUserId();
	}
	
	$.ajax({
		type: "post",
		url: "/dingding-web/moonActivityAction!checkIsShare.action",
		dataType: "jsonp",
		async: false,
		jsonp: "callback",
		jsonpCallback: "success_jsop",
		data: {
			userId: userId
		},
		success: function(data) {
			//页面加载的时候首先判断该用户有没有参加过该活动
			//分享过
			if(data.responseBody.haveTakePart) {
				$('.radio input').attr({
					'disabled': true
				});
				$('.radio input').eq(parseInt(data.responseBody.gifts[0]) - 1).prop({'checked': 'checked'});
				giftId = data.responseBody.gifts[0];
				$('.share-btn').on('click', function() {
					startShare(userId, giftId);
				});
			}
			//没分享过
			else {
				$('.radio input').attr({
					'disabled': false
				});
				//默认选中第一个
				$('.radio input').eq(0).attr({
					'checked': true
				});
	
				
				$('.share-btn').on('click', function() {

		          giftId = $('.radio input:checked').attr('value');
		          var present = giftId == 1 ? '128元月饼' : '50元京东E卡';
		          swal({
		            title:'',
		            text:'您已选择'+present+'礼品，确认后无法更改!',
		            type: 'warning',
		            showCancelButton:"true",  
	                showConfirmButton:"true",  
	                confirmButtonText:"确认",  
	                cancelButtonText:"取消"
		          },function(){
		            startShare(userId, giftId);
		            $('.radio input').attr({
		              'disabled': true
		            });
		          });

		          
		        });
			}
		}
	});

	//发起分享
	function startShare(userId, giftId) {
		$.ajax({
			type: "post",
			url: "/dingding-web/moonActivityAction!startShare.action",
			dataType: "jsonp",
			async: false,
			jsonp: "callback",
			jsonpCallback: "success_jsop",
			data: {
				userId: userId,
				giftId: giftId
			},
			success: function(data) {
				
				if(isIOS()) {
					
					window.webkit.messageHandlers.takeActivityShare.postMessage({
						"title": "叮叮约车",
						"content": data.responseBody.activityTitle,
						"url": data.responseBody.activityUrl
					});
					//1是分享给朋友 ，2是分享到朋友圈 
//					window.webkit.messageHandlers.activityShareByType.postMessage({
//						"type" : [1],
//						"title": "叮叮约车",
//						"content": data.responseBody.activityTitle,
//						"url": data.responseBody.activityUrl
//					});
				} else {
					JavascriptInterface.takeActivityShare('叮叮约车', data.responseBody.activityTitle, data.responseBody.activityUrl);
//					JavascriptInterface.activityShareByType([1],'叮叮约车', data.responseBody.activityTitle, data.responseBody.activityUrl);
				}
			}
		});
	}
})
